package com.biim.testsms.picture;

import java.util.Set;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;

import com.biim.testsms.PreferenceKey;

public abstract class SenserActivity extends Service implements
		SensorEventListener {

	private long lastTime;
	private float speed;
	private float lastX;
	private float lastY;
	private float lastZ;
	private float x, y, z;

	private int SHAKE_THRESHOLD = 800;

	private static final int DATA_X = SensorManager.DATA_X; // 0
	private static final int DATA_Y = SensorManager.DATA_Y; // 1
	private static final int DATA_Z = SensorManager.DATA_Z; // 2

	private SensorManager sensorManager;
	private Sensor accelerormeterSensor;
	/*
	 * service?
	 *  
	 */
	protected abstract void onDataReceived(boolean Event,float X,float Y,float Z);
	protected abstract void onSensorData(float X,float Y,float Z);
	

//	protected void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		this.SHAKE_THRESHOLD = get_SHAKE_THRESHOLD();
//		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
//		accelerormeterSensor = sensorManager
//				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//	}

	private int get_SHAKE_THRESHOLD()
	{
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		int THRESHOLD_VALUE = sharedPreferences.getInt(PreferenceKey.KEY_SHAKE_THRESHOLD, 0);
		return THRESHOLD_VALUE;
	}

//	@Override
//	public void onStart() {
//		super.onStart();
//		if (accelerormeterSensor != null)
//			sensorManager.registerListener(this, accelerormeterSensor,
//					SensorManager.SENSOR_DELAY_GAME);
//	}

//	@Override
//	public void onStop() {
//		super.onStop();
//		if (sensorManager != null)
//			sensorManager.unregisterListener(this);
//	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		//start
		
		return START_REDELIVER_INTENT ;
	}
	
	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			long currentTime = System.currentTimeMillis();
			long gabOfTime = (currentTime - lastTime);
			if (gabOfTime > 100) {
				lastTime = currentTime;
				x = event.values[SensorManager.DATA_X];
				y = event.values[SensorManager.DATA_Y];
				z = event.values[SensorManager.DATA_Z];
				onSensorData(x, y, z);
				speed = Math.abs(x + y + z - lastX - lastY - lastZ) / gabOfTime
						* 10000;
				if (SHAKE_THRESHOLD > 500 && speed > SHAKE_THRESHOLD) {
					try {
						// only used try/catch block
						// maybe use Thread
						onDataReceived(true,x,y,z);
					} catch (Exception e) {
						e.printStackTrace();
					}
					/*
					 * float[] f = event.values; for (int i = 0; i < f.length;
					 * i++) { // Toast.makeText(this , String.valueOf(f[i]),
					 * Toast.LENGTH_LONG).show(); }
					 */
				}

				lastX = event.values[DATA_X];
				lastY = event.values[DATA_Y];
				lastZ = event.values[DATA_Z];
			}

		}

	}
}